// LiteLoader-AIDS automatic generated
/// <reference path="d:\Game\MCBE\Server\LiteloaderSE/dts/helperlib/src/index.d.ts"/> 

import { Form, menu_end, menu_page } from '../utils/formClass.js';
import * as utils from '../utils/utils.js';
import { CONF_CONFIG, PLAYERINFO_CONFIG} from '../config.js';
import { XYZ_PUBLIC, XYZ_PERSON } from '../module/xyzData.js';
import { ReceivedXyz } from '../const.js';

export function mainMenu(pl){
    function content(pl){
        if (pl.blockPos.dimid == 0){
            return `\n§9当前坐标:§r ${utils.buildPlXyzStr(pl)}\n§7(对应下届坐标: §c${Math.floor(pl.blockPos.x/8)}§7, §c~§7, §c${Math.floor(pl.blockPos.z/8)}§7)\n `;
        }else if (pl.blockPos.dimid == 1){
            return `\n§9当前坐标:§r ${utils.buildPlXyzStr(pl)}\n§7(对应主世界坐标: §a${Math.floor(pl.blockPos.x*8)}§7, §a~§7, §a${Math.floor(pl.blockPos.z*8)}§7)\n `;
        }else {
            return `\n§9当前坐标:§r ${utils.buildPlXyzStr(pl)}\n `;
        }
    }
    let formConfig = [
        ['setTitle', [`§l坐标`]],
        ['setContent', [content(pl)]],
        ['addButton', ["§3§l当前坐标", 'textures/ui/freeze_heart_flash'], myPos],
        ['addButton', ["§2§l记录当前坐标", 'textures/ui/icon_book_writable'], record],
        ['addButton', ["§l查看个人坐标记录", 'textures/ui/Friend1'], (pl, id)=>queryCategories(pl, pl.xuid, true)],
        ['addButton', [`§l查看公共坐标记录`, 'textures/ui/icon_multiplayer'], (pl, id)=>queryCategories(pl, pl.xuid, false)],
        ['addButton', [`§l个人设置`, 'textures/ui/icon_setting'], (pl, id)=>setting(pl, pl.xuid, true)],
    ];
    if (utils.navigation.has(pl)){
        formConfig.push(['addButton', [`§c§l■ 终止当前导航`, 'textures/ui/Ping_Offline_Red'], utils.navigation.stop]);
    }
    if (pl.isOP()){
        formConfig.push(['addButton', [`§l§c管理员设置`, 'textures/ui/icon_setting'], (pl, id)=>setting(pl, pl.xuid, false)]);
    }
    formConfig.push(['close', ()=>{}]);
    new Form('newSimpleForm', formConfig).sendForm(pl);
}

// 当前坐标
function myPos(pl){
    let formConfig = [
        ['setTitle', ['§l发送当前坐标到..']],
        ['setContent', [`\n§9发送当前坐标: §r${utils.buildPlXyzStr(pl)}\n `]],
        ['addButton', [`§3§l广播坐标给所有人`], broadcast],
        ['addButton', ['§2§l导航收到的坐标\n' + (ReceivedXyz[pl.xuid]==undefined ? `§r§l§o(没有收到坐标)` : `${utils.buildXyzStr(ReceivedXyz[pl.xuid])}`)], navigationXyz],
    ];
    let pls = mc.getOnlinePlayers();
    pls = pls.filter(pl_1=>!pl_1.isSimulatedPlayer());
    for (let pl_1 of pls){
        formConfig.push(['addButton', [`发送给: ${pl_1.realName}`], sendXyz]);
    }
    formConfig.push(['close', mainMenu]);
    new Form('newSimpleForm', formConfig).sendForm(pl);
    
    // 广播坐标给所有人
    function broadcast(pl){
        pl.talkAs(`§e我在这里: §r${utils.buildPlXyzStr(pl)}`);
        pls.forEach(pl_1=>{
            if (pl_1.xuid == pl.xuid){return;}
            ReceivedXyz[pl_1.xuid] = {name: pl.realName, pos: pl.blockPos};
        });
        return;
    }

    // 导航收到的坐标
    function navigationXyz(pl){
        if (ReceivedXyz[pl.xuid] == undefined){
            return menu_end(pl, "§a§l导航收到的坐标", '§c你没有收到过坐标', mainMenu(pl));
        }
        utils.navigation.start(pl, ReceivedXyz[pl.xuid]);
        return;
    }

    // 发送给
    function sendXyz(pl, id){
        if (pls[id-2] == null){
            return pl.tell(`§c发送失败, 玩家已下线`);
        }
        if (pls[id-2].xuid != pl.xuid){ 
            pls[id-2].tell(`§a<${pl.realName}§a> §r私聊:\n§e我在这里: §r${utils.buildPlXyzStr(pl)}`); // 对方收到私聊
        }
        pl.tell(`§a<${pls[id-2].realName}§a> §r私聊:\n§e我在这里: §r${utils.buildPlXyzStr(pl)}`); // 自己提示私聊
        ReceivedXyz[pls[id-2].xuid] = {name: pl.realName, pos: pl.blockPos};
    }
}

// 记录当前坐标
function record(pl){
    let CONF_TP = CONF_CONFIG.get("额外功能")["传送坐标"];
    let CONF_NOTE = CONF_CONFIG.get('坐标记录');
    let formConfig = [
        ['setTitle', ['§l记录当前坐标']],
        ['addLabel', [`\n§9当前坐标: §r${utils.buildPlXyzStr(pl)}\n `]],
        ['addInput', [`§l坐标名称: §r§7(个人剩余: ${CONF_NOTE['个人坐标数量上限'] - XYZ_PERSON.count(pl.xuid)}个)`], "posName"],
    ];
    
    if (pl.isOP() || CONF_TP["允许记录个人传送点"] || CONF_TP["允许记录公共传送点"]){
        if (CONF_TP["个人坐标传送点上限"] != -1){
            formConfig.push(['addSwitch', [`记录为传送点坐标 §7(个人传送点剩余: ${CONF_TP['个人传送点上限'] - XYZ_PERSON.countTp(pl.xuid)})`], "isTpPos"]);
        }else{
            formConfig.push(['addSwitch', [`记录为传送点坐标`, false], "isTpPos"]);
        }
    }

    formConfig.push(['addDropdown', ['选择个人坐标分类', XYZ_PERSON.listCategory(pl.xuid), 0], "personCategory"]);
    formConfig.push(['addSwitch', ['§e记录到公共坐标 §7(不会记录到个人坐标)', false], "isPulic"]);
    formConfig.push(['addDropdown', ['§e选择公共坐标分类', XYZ_PUBLIC.listCategory(), 0], "publicCategory"]);
    formConfig.push(['commit', commit]);
    formConfig.push(['close', mainMenu]);
    new Form('newCustomForm', formConfig).sendForm(pl);
    function commit(pl, args){
        args['isTpPos'] ??= false;
        if (args['posName'] == ''){
            return menu_end(pl, "§l记录当前坐标", '记录当前坐标: §c坐标名称不能为空', record);
        }
        let warp = {
            name: args['posName'],
            tpAllow: args['isTpPos'],
            time: new Date().getTime(),
            //tpPrice: args['isTpPos'] ? CONF_TP['个人传送价格'] : CONF_TP['公共传送价格'],
            pos: {
                x: pl.blockPos.x, 
                y: pl.blockPos.y, 
                z: pl.blockPos.z, 
                dimid: pl.blockPos.dimid
            }
        };
        let posStr = utils.buildXyzStr(warp);
        // 记录个人坐标
        if (args['isPulic'] === false){
            let category = XYZ_PERSON.listCategory(pl.xuid)[args['personCategory']];
            if (args['isTpPos'] === true){
                if (!pl.isOP() && CONF_TP["允许记录个人传送点"] === false){
                    return menu_end(pl, '§l记录当前坐标', `§c记录失败, 禁止记录个人传送点`, record);
                }
                if (XYZ_PERSON.countTp(pl.xuid) >= CONF_TP['个人传送点上限']){
                    return menu_end(pl, '§l记录当前坐标', `§c记录失败, 个人传送点坐标数量达到上限(最大为 ${CONF_TP['个人传送点上限']} )\n\n你可以删除无用坐标以节省空间\n\n${posStr}`, record);
                }
            }
            if (XYZ_PERSON.count(pl.xuid) >= CONF_NOTE['个人坐标数量上限']){
                return menu_end(pl, '§l记录当前坐标', `§c记录失败, 个人坐标数量达到上限(最大为 ${CONF_NOTE['个人坐标数量上限']} )\n\n你可以删除无用坐标以节省空间\n\n${posStr}`, record);
            }
            
            let res = XYZ_PERSON.addPos(category, warp, pl.xuid);
            if (res === false){
                return menu_end(pl, '§l记录当前坐标', `§c记录失败, 重复的坐标名称`, record);
            }
            return menu_end(pl, "§l记录当前坐标",`§b§l个人坐标记录成功\n\n§7${category}/§r\n ${posStr}`, record);
        }

        // 记录公共坐标
        if (!pl.isOP() && CONF_NOTE["允许玩家记录公共坐标"] === false){
            return menu_end(pl, '§l记录当前坐标', `§c记录失败, 仅管理员可记录公共坐标`, record);
        }
        let category = XYZ_PUBLIC.listCategory(pl.xuid)[args['publicCategory']];
        if (args['isTpPos'] === true){
            if (!pl.isOP() && CONF_TP["允许记录公共传送点"] === false){
                return menu_end(pl, '§l记录当前坐标', `§c记录失败, 禁止记录公共传送点`, record);
            }
            if (XYZ_PUBLIC.countTp() >= CONF_TP['公共传送点上限']){
                return menu_end(pl, '§l记录当前坐标', `§c记录失败, 公共传送点坐标数量达到上限(最大为 ${CONF_TP['公共传送点上限']} )\n\n你可以联系管理员删除无用坐标以节省空间\n\n${posStr}`, record);
            }
        }
        if (XYZ_PUBLIC.count(pl.xuid) >= CONF_NOTE['公共坐标数量上限']){
            return menu_end(pl, '§l记录当前坐标', `§c记录失败, 公共坐标数量达到上限(最大为 ${CONF_NOTE['公共坐标数量上限']} )\n\n你可以联系管理员删除无用坐标以节省空间\n\n${posStr}`, record);
        }
        let res = XYZ_PUBLIC.addPos(category, warp, pl.xuid);
        if (res === false){
            return menu_end(pl, '§l记录当前坐标', `§c记录失败, 重复的坐标名称`, record);
        }
        mc.broadcast(`§a<${pl.realName}> §b记录了公共坐标:§r ${category}/${posStr}`);
        logger.info(`<${pl.realName}> 记录了公共坐标: ${category}/${posStr}`);
        return menu_end(pl, "§l记录当前坐标",`§b§l公共坐标记录成功\n\n§7${category}/§r\n ${posStr}`, record);
    }
}

// 查看坐标记录
/**
 * @description: 查看坐标记录
 * @param {Player} pl 表单所有者
 * @param {string|undefined} xuid 坐标所有者
 * @param {boolean} isPerson 是否是个人坐标
 * @return {undefined}
 */
function queryCategories(pl, xuid, isPerson){
    let xyzData = isPerson ? XYZ_PERSON.get(xuid) : XYZ_PUBLIC.get();
    let categories = Object.keys(xyzData);
    let formConfig = [
        ['setTitle', [isPerson ? '§l选择分类 - 个人坐标' : '§l选择分类 - 公共坐标']],
        ['setContent', [`\n  §a选择一个分类进行查看:\n `]]
    ];
    for (let i = 0; i<categories.length; i++){
        formConfig.push(
            [
                'addButton', 
                [`§l${categories[i]} §r§l(${xyzData[categories[i]].length})`], 
                (pl, id)=>queryWarp(pl, xuid, isPerson, categories[id])
            ]
        );
    }
    formConfig.push(['close', mainMenu]);
    new Form('newSimpleForm', formConfig).sendForm(pl);
}

function queryWarp(pl, xuid, isPerson, category){
    let xyzData = isPerson ? XYZ_PERSON.get(xuid) : XYZ_PUBLIC.get();
    let formConfig = [
        ['setTitle', [isPerson ? `${category} - 个人坐标` : `${category} - 公共坐标`]],
    ];
    let i = 0;
    let content = xyzData[category].map(warp=>{
        return `§r§d# ${++i} §7| ${utils.buildXyzStr(warp)}`;
    }).join('\n');
    formConfig.push(['addLabel', [content]]);
    formConfig.push(['addInput', ['§6■ 选择坐标并执行功能', '请输入坐标序号: 正整数'], 'warpIndex']);
    formConfig.push(['commit', getWarp]); //f=============
    formConfig.push(['close', (pl)=>queryCategories(pl, xuid, isPerson)]);
    new Form('newCustomForm', formConfig).sendForm(pl);

    function getWarp(pl, args){
        let warpIndex = args['warpIndex'];
        let xyzList = xyzData[category];
    
        // 空判断
        if (warpIndex == ""){
            return queryCategories(pl, xuid, isPerson);
        }
    
        // 正整数判断
        if (!(/(^[1-9]\d*$)/.test( Number(warpIndex) ))){
            return menu_end(pl, "§l选择坐标并执行功能", "§cERROR 请输入正整数", (pl)=>queryWarp(pl, xuid, isPerson, category));
        }
    
        // 编号超出判断
        if (Number(warpIndex) > xyzList.length){
            let title = '§l选择坐标并执行功能';
            let endmsg = `§cERROR 序号为 ${warpIndex} 的记录不存在`;
            return menu_end(pl, title, endmsg, (pl)=>queryWarp(pl, xuid, isPerson, category));
        }
    
        let warp = xyzList[Number(warpIndex)-1];
        if (isPerson){
            warp = {...warp, ...PLAYERINFO_CONFIG.get('players')[xuid]};
        }

        return executeWarp(pl, xuid, isPerson, category, warp);
    }
}

function executeWarp(pl, xuid, isPerson, category, warp){
    let CONF_TP = CONF_CONFIG.get("额外功能")["传送坐标"];
    let formConfig = [
        ['setTitle', ['§l选择坐标并执行功能']],
        ['setContent', [`\n  §9已选择坐标: §r${utils.buildXyzStr(warp)}\n  §7记录时间: ${utils.getTimeStr(warp.time)}\n  ` + (isPerson ? '' : `§7记录者: ${warp.realName} \n `)]],
        ['addButton', ['§l● 私聊该坐标'], (pl, id)=>msgWarp(pl, xuid, isPerson, category, warp)],
        ['addButton', ['§3§l☁ 广播该坐标'], (pl, id)=>broadcastWarp(pl, xuid, isPerson, category, warp)],
        ['addButton', ['§l§c✖ 删除该坐标'], (pl, id)=>deleteWarp(pl, xuid, isPerson, category, warp)],
        ['addButton', ['§2§l▣ 导航该坐标'], (pl, id)=>navigateWarp(pl, xuid, isPerson, category, warp)],
    ];

    if (!isPerson){
        formConfig.push(['addButton', ['§6§l★ 收藏该坐标'], (pl, id)=>collectWarp(pl, xuid, isPerson, category, warp)]);
    }
    if (pl.isOP() || (warp.tpAllow === true && (isPerson ? CONF_TP['允许传送个人传送点']===true : CONF_TP['允许传送公共传送点']===true))){
        formConfig.push(['addButton', ['§d§l✈ 传送到坐标'], (pl, id)=>tpWarp(pl, xuid, isPerson, category, warp)]);
    }
    formConfig.push(['close', (pl)=>queryWarp(pl, xuid, isPerson, category)]);
    new Form('newSimpleForm', formConfig).sendForm(pl);
}

// 坐标功能项
function msgWarp(pl, xuid, isPerson, category, warp){
    let formConfig = [
        ['setTitle', ['§l私聊该坐标']],
        ['setContent', [`\n  §9已选择坐标: §r${utils.buildXyzStr(warp)}\n`]],
    ];
    let pls = mc.getOnlinePlayers();
    pls = pls.filter(pl_1=>!pl_1.isSimulatedPlayer());
    for (let i = 0; i<pls.length; i++){
        formConfig.push(['addButton', [pls[i].realName], msg]);
    }
    formConfig.push(['close', (pl)=>executeWarp(pl, xuid, isPerson, category, warp)]);

    new Form('newSimpleForm', formConfig).sendForm(pl);

    function msg(pl, id){
        if (pls[id].xuid == pl.xuid){ //私聊自己
            pl.tell(`§a<${pls[id].realName}§a> §r私聊:\n${utils.buildXyzStr(warp)}`);
            ReceivedXyz[pl.xuid] = JSON.parse(JSON.stringify(warp));
            return;
        }
        if (pls[id] != null){
            if (pls[id].sendText(`§a<${pl.realName}§a> §r私聊:\n${utils.buildXyzStr(warp)}`) == true){
                ReceivedXyz[pls[id].xuid] = JSON.parse(JSON.stringify(warp));
                pl.tell(`§a<${pls[id].realName}§a> §r私聊:\n${utils.buildXyzStr(warp)}`);
            }else{
                pl.tell(`§c发送失败, 玩家已下线`);
            }
        }else{
            pl.tell(`§c发送失败, 玩家已下线`);
        }
    }
}

function broadcastWarp(pl, xuid, isPerson, category, warp){
    pl.talkAs(utils.buildXyzStr(warp));
    let pls = mc.getOnlinePlayers();
    pls = pls.filter(pl_1=>!pl_1.isSimulatedPlayer());
    pls.forEach((pl_1)=>{
        if (pl_1 == null){return;}
        ReceivedXyz[pl_1.xuid] = JSON.parse(JSON.stringify(warp));
    });
    return;
}

function deleteWarp(pl, xuid, isPerson, category, warp){
    let formConfig = [
        ['setTitle', ['§c§l删除该坐标']],
        ['setContent', [`\n  §c确认删除坐标:\n\n    ${utils.buildXyzStr(warp)}\n `]],
        ['addButton', ['§c§l确认删除'], deleteConfirm],
        ['addButton', ['§l取消'], (pl, id)=>executeWarp(pl, xuid, isPerson, category, warp)]
    ];
    formConfig.push(['close', (pl)=>executeWarp(pl, xuid, isPerson, category, warp)]);
    new Form('newSimpleForm', formConfig).sendForm(pl);

    function deleteConfirm(pl, id){
        if (isPerson === true){
            let res = XYZ_PERSON.removePos(category, warp.name, xuid);
            if (res === false){
                return menu_end(pl, '§c§l删除该坐标', `§cERROR 不存在此名称的坐标`, (pl)=>executeWarp(pl, xuid, isPerson, category, warp));
            }
            return menu_end(pl, "§c§l删除该坐标", `§a个人坐标删除成功\n  §r${utils.buildXyzStr(warp)}`, (pl)=>queryWarp(pl, xuid, isPerson, category));
        }

        if ((!pl.isOP()) && pl.xuid != warp.xuid){
            return menu_end(pl, '§c§l删除该坐标', `§cERROR 该公共坐标由 <${warp.realName}> 记录, 你无权删除它`, (pl)=>executeWarp(pl, xuid, isPerson, category, warp));
        }
        let res = XYZ_PUBLIC.removePos(category, warp.name, xuid);
        if (res === false){
            return menu_end(pl, '§c§l删除该坐标', `§cERROR 不存在此名称的坐标`, (pl)=>executeWarp(pl, xuid, isPerson, category, warp));
        }
        logger.info(`<${pl.realName}> 删除了公共坐标: ${category}/${utils.buildXyzStr(warp)}`);
        mc.broadcast(`§a<${pl.realName}> §c删除了公共坐标:§r ${category}/${utils.buildXyzStr(warp)}`);
        return menu_end(pl, "§c§l删除该坐标", `§a公共坐标删除成功\n  §r${utils.buildXyzStr(warp)}`, (pl)=>queryWarp(pl, xuid, isPerson, category));
    }
}

function navigateWarp(pl, xuid, isPerson, category, warp){
    utils.navigation.start(pl, warp);
}

function collectWarp(pl, xuid, isPerson, category, warp){
    let CONF_NOTE = CONF_CONFIG.get('坐标记录');
    let xyzData = isPerson ? XYZ_PERSON.get(xuid) : XYZ_PUBLIC.get();
    let categories = Object.keys(xyzData);

    let formConfig = [
        ['setTitle', ['§l收藏该坐标']],
        ['addLabel', [`\n  §9已选择坐标: §r${utils.buildXyzStr(warp)}\n`]],
        ['addDropdown', ['§l选择收藏到分类', categories], 'categoryIndex'],
        ['commit', callback],
        ['close', (pl)=>executeWarp(pl, xuid, isPerson, category, warp)]
    ];
    new Form('newCustomForm', formConfig).sendForm(pl);
    
    function callback(pl, args){
        let category = categories[args['categoryIndex']];
        if (XYZ_PERSON.count(xuid) >= CONF_NOTE['个人坐标数量上限']){
            return menu_end(pl, '§l记录当前坐标', `§c收藏失败, 个人坐标数量达到上限(最大为 ${CONF_NOTE['个人坐标数量上限']} )\n\n你可以删除无用坐标以节省空间\n\n${utils.buildXyzStr(warp)}`, (pl)=>executeWarp(pl, xuid, isPerson, category, warp));
        }
        let _warp = {
            name: warp.name,
            tpAllow: false,
            time: new Date().getTime(),
            pos: warp.pos,
        };
        let res = XYZ_PERSON.addPos(category, _warp, xuid);
        if (res === false){
            return menu_end(pl, '§l记录当前坐标', `§c收藏失败, 重复的坐标名称`, (pl)=>executeWarp(pl, xuid, isPerson, category, warp));
        }
        return menu_end(pl, "§l记录当前坐标",`§b§l收藏坐标成功\n\n§7${category}/§r\n ${utils.buildXyzStr(_warp)}`, (pl)=>executeWarp(pl, xuid, isPerson, category, warp));
    }
}

function tpWarp(pl, xuid, isPerson, category, warp){
    pl.teleport(warp.pos.x, warp.pos.y, warp.pos.z, warp.pos.dimid);
    pl.setTitle(warp.name);
}


// 个人公共设置
function setting(pl, xuid, isPerson){
    let formConfig = [
        ['setTitle', [isPerson?'§l个人设置':'§l§c管理员设置']],
        ['setContent', [isPerson?'\n§l§e管理个人坐标分类':'\n§l§b管理公共坐标分类']],
        ['addButton', ['§l添加坐标分类'], (pl, id)=>addCategory(pl, xuid, isPerson)],
        ['addButton', ['§l设置分类'], (pl, id)=>setCategory(pl, xuid, isPerson)],
        ['addButton', ['§l删除分类'], (pl, id)=>removeCategory(pl, xuid, isPerson)],
        ['close', mainMenu]
    ];
    new Form('newSimpleForm', formConfig).sendForm(pl);
}

function addCategory(pl, xuid, isPerson){
    let xyzData = isPerson ? XYZ_PERSON.get(xuid) : XYZ_PUBLIC.get();
    let title = (isPerson?'§l个人坐标':'§l公共坐标')+'-添加坐标分类';
    let formConfig = [
        ['setTitle', [title]],
        ['addLabel', [``]],
        ['addInput', ['新类别名称', '请输入新类别名称'], 'categoryName'],
        ['commit', callback],
        ['close', (pl)=>setting(pl, xuid, isPerson)]
    ];
    new Form('newCustomForm', formConfig).sendForm(pl);

    function callback(pl, args){
        if (args['categoryName'] == ''){
            return setting(pl, xuid, isPerson);
        }
        if (xyzData[args['categoryName']] != undefined){
            return menu_end(pl, title, `§cERROR 名为 ${args['categoryName']} 的类别已存在`, (pl)=>addCategory(pl, xuid, isPerson));
        }
        if (isPerson === true){
            XYZ_PERSON.addCategory(args['categoryName'], xuid);
        }else{
            XYZ_PUBLIC.addCategory(args['categoryName'], xuid);
        }
        return menu_end(pl, title, `§a已添加新${(isPerson?'个人坐标':'公共坐标')}分类:§r ${args['categoryName']}`, (pl)=>setting(pl, xuid, isPerson));
    }
}

function setCategory(pl, xuid, isPerson){
    let xyzData = isPerson ? XYZ_PERSON.get(xuid) : XYZ_PUBLIC.get();
    let categories = Object.keys(xyzData);
    let title = (isPerson?'§l个人坐标':'§l公共坐标')+'-设置坐标分类';
    let formConfig = [
        ['setTitle', [title]],
        ['addLabel', [``]],
        ['addDropdown', ['选择类别', categories], 'categoryIndex'],
        ['addInput', ['修改该分类名称'], 'categoryNewName'],
        ['commit', callback],
        ['close', (pl)=>setting(pl, xuid, isPerson)]
    ];
    new Form('newCustomForm', formConfig).sendForm(pl);

    function callback(pl, args){
        let category = categories[args['categoryIndex']];
        if (args['categoryNewName'] == '' || args['categoryNewName'] == category){
            return setting(pl, xuid, isPerson);
        }
        if (isPerson === true){
            XYZ_PERSON.renameCategory(category, args['categoryNewName'], xuid);
        }else{
            XYZ_PUBLIC.renameCategory(category, args['categoryNewName'], xuid);
        }
        return menu_end(pl, title, `§a已重命名${(isPerson?'个人坐标':'公共坐标')}分类:§r\n\n ${category} --> ${args['categoryNewName']}`, (pl)=>setting(pl, xuid, isPerson));
    }
}

function removeCategory(pl, xuid, isPerson){
    let xyzData = isPerson ? XYZ_PERSON.get(xuid) : XYZ_PUBLIC.get();
    let categories = Object.keys(xyzData);
    let title = (isPerson?'§l个人坐标':'§l公共坐标')+'-§c删除坐标分类';
    let formConfig = [
        ['setTitle', [title]],
        ['addLabel', [``]],
        ['addDropdown', ['选择类别', categories], 'categoryIndex'],
        ['addSwitch', ['删除该分类', false], 'isDelete'],
        ['commit', callback],
        ['close', (pl)=>setting(pl, xuid, isPerson)]
    ];
    new Form('newCustomForm', formConfig).sendForm(pl);
    function callback(pl, args){
        let category = categories[args['categoryIndex']];
        if (args['isDelete'] === false){
            return setting(pl, xuid, isPerson);
        }
        if (categories.length == 1){
            return menu_end(pl, title, `§cERROR 无法删除, 坐标记录至少需要一个坐标分类`, (pl)=>setting(pl, xuid, isPerson));
        }
        pl.sendModalForm(title, `\n§e确认删除${(isPerson?'个人坐标':'公共坐标')}分类§r ${category}?\n§c该类别下所有记录§7(${xyzData[category].length}条)§c都会被清除`, "§c确认删除", "§l取消", (pl, res)=>{
            if (res === true){
                if (isPerson === true){
                    XYZ_PERSON.removeCategory(category, xuid);
                }else{
                    XYZ_PUBLIC.removeCategory(category, xuid);
                }
                return menu_end(pl, title, `§a已删除${(isPerson?'个人坐标':'公共坐标')}坐标分类: ${category}`, (pl)=>setting(pl, xuid, isPerson));
            }
            return setting(pl, xuid, isPerson);
        });
    }
}

// (isPerson?'§l个人坐标':'§l公共坐标')+'-添加坐标分类']